from gibe.plugin import Plugin

from gibethemenxsy200901.widgets import *
from gibethemenxsy200901.controllers import *
from gibe.controllers import date_expand_day, date_expand_month, date_expand_year

import cherrypy

class GibeThemeNxsy200901Plugin(Plugin):
    def enabled(self):
        return bool(cherrypy.config.get('gibe.theme', None) == "nxsy200901")

    def map_routes(self, blog, mapper, controllers):
        controllers['gibethemenxsy200901.Nxsy200901Controller'] = Nxsy200901Controller()
        for m in mapper:
            if m[0] == "atom1_0":
                atom_route_tuple = m
                break
        else:
            # Can't find "posts" route - guess we can't augment it then...
            return

        atom_route = list(atom_route_tuple)

        atom_route[1] = "fbatom1.0"

        i = mapper.index(atom_route_tuple)
        mapper[i] = atom_route

        to_remove = []
        for m in mapper:
            if m[0].startswith("archives"):
                to_remove.append(mapper.index(m))
            if m[0].startswith("tags"):
                to_remove.append(mapper.index(m))

        for i in sorted(to_remove, reverse = True):
            mapper.pop(i)

        mapper.append(('archives_day', 'archives/:year/:month/:day/', dict(controller='gibethemenxsy200901.Nxsy200901Controller', action='archives', _filter=date_expand_day)))
        mapper.append(('archives_month', 'archives/:year/:month/', dict(controller='gibethemenxsy200901.Nxsy200901Controller', action='archives', _filter=date_expand_month)))
        mapper.append(('archives_year', 'archives/:year/', dict(controller='gibethemenxsy200901.Nxsy200901Controller', action='archives', _filter=date_expand_year)))
        mapper.append(('archives', 'archives', dict(controller='gibethemenxsy200901.Nxsy200901Controller', action='archives')))
        mapper.append(('tags', 'tags/:(tagname)', dict(controller='gibethemenxsy200901.Nxsy200901Controller', action='tag')))
        mapper.append(('taglist', 'tags/', dict(controller='gibethemenxsy200901.Nxsy200901Controller', action='taglist')))


    def template_search_path(self, blog, search_path):
        import gibethemenxsy200901.templates
        search_path.insert(0, gibethemenxsy200901.templates.__path__[0])

    def template_data(self, blog, template_data):
        td = {
            'theme': GibeThemeNxsy200901,
        }
        template_data.update(td)

    def template_replacements(self, blog, template_replacements):
        template_replacements['genshi:gibe.templates.frontpage'] = 'genshi:gibethemenxsy200901.templates.frontpage'
        template_replacements['genshi:gibe.templates.post'] = 'genshi:gibethemenxsy200901.templates.post'
        template_replacements['genshi:gibe.templates.comment_preview'] = 'genshi:gibethemenxsy200901.templates.comment_preview'
        template_replacements['genshi:gibe.templates.archives'] = 'genshi:gibethemenxsy200901.templates.archives'
        template_replacements['genshi:gibe.templates.not_found'] = 'genshi:gibethemenxsy200901.templates.not_found'
        pass
